﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;
//using hnrzxt.tool.md5;

namespace hnrzxt.Controllers
{
    public class UserController : Controller
    {
        private readonly ApplicationDbContext _db;
        public UserController(ApplicationDbContext db)
        {
            _db = db;
        }
        #region 查询
        public IActionResult Index()
        {
            IEnumerable<User> users = _db.Users;
            
            return View(users);
        }
        #endregion

        #region 添加
        public IActionResult Create() //添加用户界面
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(User user)
        {
            if (user.Name == user.Password)
            {
                ModelState.TryAddModelError("error", "为了您的安全考虑，用户名和密码不能一样");
            }
            if (ModelState.IsValid)
            {
                _db.Users.Add(user);
                //user.Password.MD();
                _db.SaveChanges();
                return RedirectToAction("index");
            }
            return View(user);
        }
        #endregion

        #region 编辑
        //get
        public IActionResult Edit(int? id)
        {
            if(id == null|| id == 0)
            {
                return NotFound();
            }
            var userFromDb = _db.Users.Find(id);

            if(userFromDb == null)
            {
                return NotFound();
            }
            return View(userFromDb);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(User user)
        {
            if(user.Name == user.Password)
            {
                ModelState.TryAddModelError("error", "为了您的安全考虑，用户名和密码不能一样");
            }
            if (ModelState.IsValid)
            {
                _db.Users.Update(user);
                _db.SaveChanges();
                return RedirectToAction("index");
            }
            return View(user);
        }
        #endregion

        #region 删除
        //get
        public IActionResult Delete(int? id)
        {
            if(id== null|| id == 0)
            {
                return NotFound();
            }
            var userFormDb = _db.Users.Find(id);
            if(userFormDb == null)
            {
                return NotFound();
            }
            return View(userFormDb);
        }

        [HttpPost,ActionName("Delete")]
        [ValidateAntiForgeryToken]

        public IActionResult DeletePost(int? id)
        {
            var user = _db.Users.Find(id);
            if(user == null)
            {
                return NotFound();
            }
            _db.Users.Remove(user);
            _db.SaveChanges();
            TempData["success"] = "删除成功";
            return RedirectToAction("Index");
        }


        public List<Dictionary<string, object>> GettheUserData()
        {
            List<User> userlist = _db.Users.ToList();
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            for (int i = 0; i < userlist.Count; i++)
            {
                Dictionary<string, object> user = new Dictionary<string, object>();
                user.Add("title", userlist[i].Name);
                list.Add(user);
            }
            return list;
        }

        public IActionResult GetUserData()
        {
            return new JsonResult(GettheUserData());
        }
        #endregion
    }
}
